package com.moguhoutai.controller;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.moguhoutai.biz.LandlordBiz;
import com.moguhoutai.biz.ResourceBiz;
import com.moguhoutai.model.Admin;
import com.moguhoutai.model.Landlord;
import com.moguhoutai.model.Resources;
import com.moguhoutai.util.BusinessException;
import com.moguhoutai.util.Constants;
@Controller
@RequestMapping("/landlord")
public class LandlordController {
	@Resource
	private LandlordBiz lBiz;
	@Resource
	private ResourceBiz reBiz;
	
	 @RequestMapping("/list")
		public String list(Map<String, Object> request){
			try {
				long count = lBiz.findCount();
				List<Landlord> list = lBiz.findAll();
				request.put("totalCount", count);
				request.put("list", list);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return "/landlord/list";
		}
	  
	   @RequestMapping("/preAdd")
		public String preAdd(Map<String, Object> request){
			try {
				request.put("landlord", new Landlord());
				List<Landlord> list = lBiz.findAll();
				request.put("list", list);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return "/landlord/add";
		}
	   @ResponseBody
	   @RequestMapping("/add")
		public String add(Landlord landlord, MultipartHttpServletRequest request, HttpSession session){
			try {
				Admin admin = (Admin) session.getAttribute("currentUser");
				MultipartFile file = request.getFile("file");
				String fileName = UUID.randomUUID().toString();
				File newFile = new File(Constants.FILE_PATH + fileName);
				file.transferTo(newFile);
				Resources res = new Resources();
				res.setResId(fileName);
				res.setResName(file.getOriginalFilename());
				res.setResPath(fileName);
				res.setCreator(admin.getTrueName());
				res.setUpdator(admin.getTrueName());
				landlord.setRes(res);
				lBiz.save(landlord);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "redirect:/landlord/list";
		}
	   
	   @RequestMapping("/delete/{landId}")
	    public String  delete(@PathVariable int landId){
	    	try {
	    		Landlord landlord =lBiz.findById(landId);
	    		if(landlord==null){
	    			return "redirect:list";
	    		}
	    		lBiz.delete(landlord);
			} catch (Exception e) {
				e.printStackTrace();
				
			}
	    	return "redirect:/landlord/list";
	    }
		@RequestMapping("/preUpdate/{landId}")
		public String preUpdate(@PathVariable int landId, Map<String, Object> request){
			try {
				Landlord landlord = lBiz.findById(landId);
				request.put("landlord", landlord);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return "/landlord/update";
		}
		@ResponseBody
		@RequestMapping("/update")
		public String update(Landlord landlord, MultipartHttpServletRequest request, HttpSession session){
			try {
				MultipartFile file = request.getFile("file");
				if(file.getSize() != 0){
					String fileName = UUID.randomUUID().toString();
					File newFile = new File(Constants.FILE_PATH + fileName);
					file.transferTo(newFile);
					Resources res = new Resources();
					res.setResId(fileName);
					res.setResName(file.getOriginalFilename());
					res.setResPath(fileName);
					res.setResType(file.getContentType());
					landlord.setRes(res);
				}
				lBiz.update(landlord);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "redirect:/landlord/list";
		}
		
		
}
